package com.mickey.rabbitmq;

import com.mickey.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Desc:mq发送者
 * @Author:czx
 * @CreateDate:2019/1/11 0011$ 14:03$
 */
@Service
public class MQSender {

    private static Logger logger = LoggerFactory.getLogger(MQReceiver.class);

    @Autowired
    AmqpTemplate amqpTemplate;

    public void sendMiaoshaMessage(MiaoshaMessage mm) {
        String msg = StringUtil.beanToString(mm);
        logger.info("sendMessage:{}",msg);
        amqpTemplate.convertAndSend(MQConfig.MIAOSHA_QUEUE,msg);

    }






    public void send(Object message){
        String msg = StringUtil.beanToString(message);
        logger.info("sendMessage:{}",msg);
        amqpTemplate.convertAndSend(MQConfig.QUEUE,msg);
    }

    public void sendTopic(Object message){
        String msg = StringUtil.beanToString(message);
        logger.info("sendTopicMessage:{}",msg);
        amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE,"topic.key1",msg+"1");
        amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE,"topic.key2",msg+"2");
    }

    public void sendFanout(Object message){
        String msg = StringUtil.beanToString(message);
        logger.info("sendFanoutMessage:{}",msg);
        amqpTemplate.convertAndSend(MQConfig.FANOUT_EXCHANGE,"",msg);
    }

    public void sendHeaders(Object message){
        String msg = StringUtil.beanToString(message);
        logger.info("sendHeadersMessage:{}",msg);
        MessageProperties properties = new MessageProperties();
        properties.setHeader("header1","value1");
        properties.setHeader("header2","value2");
        Message obj = new Message(msg.getBytes(),properties);
        amqpTemplate.convertAndSend(MQConfig.HEADERS_EXCHANGE,"",obj);
    }



}
